<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* CRM in Auth */
class Crm extends Controller {

	
	function index() {
		//$this->load->vars($data);
		$this->load->view('dashboard');
	}
	
	function do_login() {		
		$user_id	= $this->input->post("username");
		$passwd 	= sha1($this->input->post("password"));
		$query		= $this->db->query("select * from users LEFT JOIN user_position ON user_position.id=users.pos_id JOIN user_group ON users.group_id = user_group.id  where username='$user_id' and password='$passwd'");
		$count		= $query->num_rows();
		if($count>0){
			$row  = $query->row_array();
            if($row['pos_id']==$this->config->item('se')):
                $query		= $this->db->query("select * from users where division_id=".$row['division_id']." AND pos_id=".$this->config->item('ta'));
                $access		= $query->row_array();
                $this->session->set_userdata('user_ta',$access['username']);
            endif;
			$auth = array(
							'user_id'	   =>	$row['username'],
                            'user_posName' =>	$row['pos_name'],
							'user_pwd'	   =>	$row['password'],
							'user_group'   =>	$row['group_id'],
							'group_name'   =>	$row['group_name'],
                            'user_pos'     =>	$row['pos_id']  
						 ); 
			$this->session->set_userdata($auth);
            $this->access_auth();
			redirect($this->config->item('dashboard'));
			
		} else {
			redirect($this->input->server('HTTP_REFERER'));
		}
	}
	
	function do_logout(){
		$this->session->sess_destroy();
		redirect($this->config->item('auth'));
	}
    
    function access_auth(){
        if($this->session->userdata('user_group')==$this->config->item('admin')):
            $access     =   array(
                                    'menu1' =>  1,
                                    'menu10'=>  0,
                                    'menu2' =>  1,
                                    'menu3' =>  1,
                                    'menu4' =>  1,
                                 );
        elseif($this->session->userdata('user_group')==$this->config->item('admin_dna')):
            $access     =   array(
                                    'menu1' =>  1,
                                    'menu10'=>  1,
                                    'menu2' =>  1,
                                    'menu3' =>  0,
                                    'menu4' =>  0,
                                 );
        elseif($this->session->userdata('user_group')==$this->config->item('co_admin')):
            $access     =   array(
                                    'menu1' =>  1,
                                    'menu10'=>  0,
                                    'menu2' =>  1,
                                    'menu3' =>  1,
                                    'menu4' =>  0,
                                 );
        elseif($this->session->userdata('user_group')==$this->config->item('manage')):
            if(($this->session->userdata('user_pos')==$this->config->item('spv')) || ($this->session->userdata('user_pos')==$this->config->item('mgr'))):
                    $access     =   array(
                                    'menu1' =>  1,
                                    'menu10'=>  0,
                                    'menu2' =>  1,
                                    'menu3' =>  1,
                                    'menu4' =>  0,
                                 ); 
            elseif($this->session->userdata('user_pos')==$this->config->item('keu')):
                   $access     =   array(
                                    'menu1' =>  0,
                                    'menu10'=>  0,
                                    'menu2' =>  0,
                                    'menu3' =>  1,
                                    'menu4' =>  0,
                                 );                                            
            endif;
        elseif($this->session->userdata('user_group')==$this->config->item('prod')):
                   $access     =   array(
                                    'menu1' =>  0,
                                    'menu10'=>  0,
                                    'menu2' =>  0,
                                    'menu3' =>  1,
                                    'menu4' =>  0,
                                 );
                                                                                          
        elseif($this->session->userdata('user_group')==$this->config->item('user')):
			
			if($this->session->userdata('user_pos')==$this->config->item('se')):
				$s=0;
			else:
				$s=1;
			endif;	
			
			$access     =   array(
                                    'menu1' =>  1,
                                    'menu10'=>  0,
                                    'menu2' =>  1,
                                    'menu3' =>  $s,
                                    'menu4' =>  0,
                                 );
        elseif($this->session->userdata('user_group')==$this->config->item('user_dna')):
            
			if($this->session->userdata('user_pos')==$this->config->item('se')):
				$s=0;
			else:
				$s=1;
			endif;	
			
			$access     =   array(
                                    'menu1' =>  1,
                                    'menu10'=>  0,
                                    'menu2' =>  1,
                                    'menu3' =>  $s,
                                    'menu4' =>  0,
                                 );                         
        elseif($this->session->userdata('user_group')==$this->config->item('user_p')):
            $access     =   array(
                                    'menu1' =>  1,
                                    'menu10'=>  0,
                                    'menu2' =>  0,
                                    'menu3' =>  0,
                                    'menu4' =>  0,
                                 );
        else:
            $access     =   array(
                                    'menu1' =>  1,
                                    'menu10'=>  0,
                                    'menu2' =>  1,
                                    'menu3' =>  1,
                                    'menu4' =>  0,
                                 );                                                     
        endif;
        
        return $this->session->set_userdata($access);
    }

}

/* End of file crm.php */